package de.g18.it0a.jlaser.main;

import java.text.MessageFormat;
import java.util.InputMismatchException;
import java.util.Scanner;

import de.g18.it0a.jlaser.domain.LaserConfiguration;
import de.g18.it0a.jlaser.domain.Mirror;
import de.g18.it0a.jlaser.domain.RoomConfiguration;
import de.g18.it0a.jlaser.nativ.menu.MainMenuEntry;
import de.g18.it0a.jlaser.nativ.menu.RoomEditMenuEntry;
import de.g18.it0a.jlaser.nativ.menu.util.MenuPrinter;
import de.g18.it0a.jlaser.solution.BruteforceSolutionManager;
import de.g18.it0a.jlaser.solution.SolutionManager;

public class NativApplicationStarter {

    public static RoomConfiguration roomConfig;


    public static void main(String[] args) {
        System.out.println("JLaser wurde gestartet!");
        printMainMenu();
    }

    public static void printMainMenu() {
        MainMenuEntry menuEntry = null;
        do {
            do {
                System.out.println("## Hauptmenü");
                MenuPrinter.getInstance().print(MainMenuEntry.class);

                int selection = -1;
                try {
                    System.out.print("Auswahl: ");
                    Scanner inputScanner = new Scanner(System.in);
                    selection = inputScanner.nextInt();
                } catch (InputMismatchException e) {
                    System.out.print("Fehlerhafte Eingabe!");
                }
                System.out.print("\n");
                menuEntry = MainMenuEntry.getByOrdinal(selection);
            } while (menuEntry == null);

            switch (menuEntry) {
                case EXIT:
                    break;

                case EDIT_ROOM:
                    RoomEditMenuEntry.printRoomEditMenu();
                    break;

                case FIND_SOLUTION:
                    SolutionManager solutionManager = new BruteforceSolutionManager(roomConfig);

                    System.out.println("Suche nach einer Lösung...");
                    LaserConfiguration solution = solutionManager.solve();
                    if (solution != null) {
                    	printWalls();
                        RoomEditMenuEntry.printRoom();
                        System.out.println("Ergebnis gefunden:");
                        System.out.print("Der Laser startet bei: " + solution.getStart());
                        for (Mirror mirrorPosition : solution.getMirrors()) {
                                        System.out.print("über:");
                                        System.out.println(mirrorPosition.getPosition().toString());
                        }
                        System.out.println("Und ended bei: " + solution.getEnd());
                    } else {
                        System.out.println("No solution found!");
                    }
                    break;
            }
        } while (menuEntry != MainMenuEntry.EXIT);
    }


    

	public static void checkIfRoomConfigExists() {
        if (roomConfig != null) {
            return;
        }

        System.out.println("1. Raum laden: ");
        System.out.println("2. Neuen Raum erstellen: ");
        System.out.println("3. Zurück: ");
        System.out.println("Auswahl: ");
        Scanner inputScanner = new Scanner(System.in);
        int choice = inputScanner.nextInt();
        switch (choice) {
            case 1:
            	RoomEditMenuEntry.loadRoomConfigFile();
                break;

            case 2:
            	RoomEditMenuEntry.newRoom();
                break;

            case 3:
                RoomEditMenuEntry.printRoomEditMenu();

            default:
            	RoomEditMenuEntry.loadRoomConfigFile();
                break;
        }
    }
    
    public static void printWalls(){
    	 System.out.println(MessageFormat.format("Wand: P1: [{0}, {1}], P2: [{2}, {3}], Laenge: {4}", "0,0", "0,0" ,"0,0",roomConfig.getHeightDouble(), roomConfig.getHeightDouble() ));
         System.out.println(MessageFormat.format("Wand: P1: [{0}, {1}], P2: [{2}, {3}], Laenge: {4}", "0,0", roomConfig.getHeightDouble() ,roomConfig.getWidthDouble(),roomConfig.getHeightDouble(), roomConfig.getWidthDouble()));
         System.out.println(MessageFormat.format("Wand: P1: [{0}, {1}], P2: [{2}, {3}], Laenge: {4}", roomConfig.getWidthDouble(), roomConfig.getHeightDouble() ,roomConfig.getWidthDouble(),"0,0", roomConfig.getHeightDouble()));
         System.out.println(MessageFormat.format("Wand: P1: [{0}, {1}], P2: [{2}, {3}], Laenge: {4}", roomConfig.getWidthDouble(), "0,0" ,"0,0","0,0", roomConfig.getWidthDouble()));
    }
}
